﻿using Abp.Domain.Values;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Common.CustomValueType
{
    [Owned]
    public class Money : ValueObject<Money>
    {
        private double _value;

        public MoneyType MoneyType { get; private set; }
        public double Value
        {
            get
            {
                return _value;
            }
            private set
            {
                _value = Math.Round(value, 2);
            }
        }
        public string Unit
        {
            get
            {
                switch (MoneyType)
                {
                    case MoneyType.RMB: return "元";
                    case MoneyType.Dollar: return "美元";
                    default: return "元";
                }
            }
        }

        public Money()
        {
            this.Value = 0;
            this.MoneyType = MoneyType.RMB;
        }

        public Money(double value, MoneyType type = MoneyType.RMB)
        {
            Value = value;
            MoneyType = type;
        }

        public static Money operator +(Money money1, Money money2)
        {
            if(money1.MoneyType != money2.MoneyType)
            {
                throw new Exception("运算操作时MoneyType不一致");
            }
            return new Money(money1.Value + money2.Value, money1.MoneyType);
        }

        public static Money operator +(Money money1, double money2Value)
        {
            return new Money(money1.Value + money2Value, money1.MoneyType);
        }

        public static Money operator -(Money money1, Money money2)
        {
            if (money1.MoneyType != money2.MoneyType)
            {
                throw new Exception("运算操作时MoneyType不一致");
            }
            return new Money(money1.Value - money2.Value, money1.MoneyType);
        }

        public static Money operator -(Money money1, double money2Value)
        {
            return new Money(money1.Value - money2Value, money1.MoneyType);
        }

        public static Money operator *(Money money1, double value)
        {
            return new Money(money1.Value * value, money1.MoneyType);
        }

        public static Money operator /(Money money1, double value)
        {
            return new Money(money1.Value / value, money1.MoneyType);
        }

        public static bool operator >(Money money1, Money money2)
        {
            if (money1.MoneyType != money2.MoneyType)
            {
                throw new Exception("运算操作时MoneyType不一致");
            }
            return money1.Value > money2.Value;
        }

        public static bool operator <(Money money1, Money money2)
        {
            if (money1.MoneyType != money2.MoneyType)
            {
                throw new Exception("运算操作时MoneyType不一致");
            }
            return money1.Value < money2.Value;
        }

        public static bool operator ==(Money money1, Money money2)
        {
            if (money1.MoneyType != money2.MoneyType)
            {
                throw new Exception("运算操作时MoneyType不一致");
            }
            return money1.Value == money2.Value;
        }

        public static bool operator !=(Money money1, Money money2)
        {
            if (money1.MoneyType != money2.MoneyType)
            {
                throw new Exception("运算操作时MoneyType不一致");
            }
            return money1.Value != money2.Value;
        }

        public static implicit operator double(Money money)
        {
            return money.Value;
        }

        public static implicit operator Money(double value)
        {
            return new Money(value);
        }

    }

    public enum MoneyType
    {
        RMB,
        Dollar
    }
}
